﻿using Epm.AppLogic.Framework;
using msBsc.AppLogic.DataAlc.cls;
using msBsc.Cls;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 地接客 备案证
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-9-14
    public class CBizRecordCard : CustBizObject
    {
        private CDmDjkRecordCard m_CDmDjkRecordCard = new CDmDjkRecordCard();
        private CDmDjkRecordCard oDmDjkRecordCard
        {
            get { return m_CDmDjkRecordCard; }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizRecordCard()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkRecordCard.DJK_RECORD_CARD);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["DJK_RECORD_CARD_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {

            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加备案证
        /// </summary>
        /// <param name="djackId">地接客Id</param>
        /// <param name="recordNumber">备案证号</param>
        /// <param name="recordPicture">备案证图片</param>
        /// <param name="businessName">业户名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地ID</param>
        /// <param name="destinationName">目的地</param>
        /// <param name="vehicleNumber">车牌号</param>
        /// <param name="vehicleType">车辆类型</param>
        /// <param name="seats">座位数</param>
        /// <param name="businessScope">经营范围</param>
        /// <param name="issuingDate">发证时间</param>
        /// <param name="publisherId">发布人</param>
        /// <returns></returns>
        public string AddRecord(string djackId, string recordNumber, string recordPicture, string businessName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string vehicleNumber, string vehicleType, string seats, string businessScope, string issuingDate, string publisherId)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(djackId) || string.IsNullOrEmpty(recordNumber) || string.IsNullOrEmpty(recordPicture) || string.IsNullOrEmpty(businessName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(vehicleNumber) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(seats) || string.IsNullOrEmpty(businessScope) || string.IsNullOrEmpty(issuingDate))
            {
                return "请完善备案证信息！";
            }

            if (recordNumber.Length > 20)
            {
                return "备案证号最大长度不得超过20个字符";
            }
            if (businessName.Length > 50)
            {
                return "业户名称不得超过50个字符";
            }
            if (vehicleNumber.Length > 10)
            {
                return "车牌号不得超过10个字符";
            }
            if (businessScope.Length > 50)
            {
                return "经营范围不得超过50个字符";
            }
            if (!CommValiCollection.IsNumber(seats))
            {
                return "座位数只能是正整数";
            }
            if (!CommValiCollection.IsDate(issuingDate))
            {
                return "发证日期格式不正确";
            }
            int vType = 1;
            int.TryParse(vehicleType, out vType);
            int vSeats = 1;
            int.TryParse(seats, out vSeats);
            #endregion

            #region 添加证件
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                DataSet ds = new CDmDjkRecordCard();
                DataRow dr = ds.Tables[0].NewRow();
                dr["DJK_RECORD_CARD_ID"] = this.DataObj.NewID();
                dr["OWNER_NAME"] = businessName;
                dr["COUNTRY_ID"] = countryId;
                dr["COUNTRY_NAME"] = countryName;
                dr["CITY_ID"] = cityId;
                dr["CITY_NAME"] = cityName;
                dr["AREA_ID"] = destinationId;
                dr["AREA_NAME"] = destinationName;
                dr["LICENSE_PLATE_NUMBER"] = vehicleNumber;
                dr["CERTIFICATE_NUMBER"] = recordNumber;
                dr["VEHICLE_TYPE"] = vType;
                dr["BLOCK_TON"] = vSeats;
                dr["OPERATING_RANGE"] = businessScope;
                dr["ISSUING_DATA"] = issuingDate;
                dr["CREATE_USERID"] = publisherId;
                dr["CREATE_DATE"] = this.dtCurrDateTime;
                dr["BELONGS_USERID"] = djackId;
                dr["IS_DELETE"] = 0;
                dr["DOCUMENT_IMAGE"] = recordPicture;
                ds.Tables[0].Rows.Add(dr);
                this.Update(ds);
                using (CBizCustomerBaseInfo baseInfo = new CBizCustomerBaseInfo())
                {
                    string[] certie = new string[] { "IS_RECORD_CARD" };
                    string ret = baseInfo.UpdateCertificatesStatus(certie, djackId, true);
                    if (ret != "true")
                    {
                        return "更新用户标识出错";
                    }
                }
                ts.Complete();
                return "OK";
            }
            #endregion
        }

        /// <summary>
        /// 修改备案证
        /// </summary>
        /// <param name="recordid">备案证Id</param>
        /// <param name="recordNumber">备案证号</param>
        /// <param name="recordPicture">备案证图片</param>
        /// <param name="businessName">业户名称</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地ID</param>
        /// <param name="destinationName">目的地</param>
        /// <param name="vehicleNumber">车牌号</param>
        /// <param name="vehicleType">车辆类型</param>
        /// <param name="seats">座位数</param>
        /// <param name="businessScope">经营范围</param>
        /// <param name="issuingDate">发证时间</param>
        /// <returns>string</returns>
        ///  Author:蒲崇彬
        ///  Creation date:2015-9-14
        public string UpdateRecord(string recordid, string recordNumber, string recordPicture, string businessName, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string vehicleNumber, string vehicleType, string seats, string businessScope, string issuingDate)
        {

            #region 数据验证
            if (string.IsNullOrEmpty(recordid) || string.IsNullOrEmpty(recordNumber) || string.IsNullOrEmpty(recordPicture) || string.IsNullOrEmpty(businessName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(vehicleNumber) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(seats) || string.IsNullOrEmpty(businessScope) || string.IsNullOrEmpty(issuingDate))
            {
                return "请完善备案证信息！";
            }

            if (recordNumber.Length > 20)
            {
                return "备案证号最大长度不得超过20个字符";
            }
            if (businessName.Length > 50)
            {
                return "业户名称不得超过50个字符";
            }
            if (vehicleNumber.Length > 10)
            {
                return "车牌号不得超过10个字符";
            }
            if (businessScope.Length > 50)
            {
                return "经营范围不得超过50个字符";
            }
            if (!CommValiCollection.IsNumber(seats))
            {
                return "座位数只能是正整数";
            }
            if (!CommValiCollection.IsDate(issuingDate))
            {
                return "发证日期格式不正确";
            }
            int vType = 1;
            int.TryParse(vehicleType, out vType);
            int vSeats = 1;
            int.TryParse(seats, out vSeats);
            #endregion
            DataSet ds = GetRecordById(recordid);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return "证件信息不存在";
            }
            DataRow dr = ds.Tables[0].Rows[0];
            dr["OWNER_NAME"] = businessName;
            dr["COUNTRY_ID"] = countryId;
            dr["COUNTRY_NAME"] = countryName;
            dr["CITY_ID"] = cityId;
            dr["CITY_NAME"] = cityName;
            dr["AREA_ID"] = destinationId;
            dr["AREA_NAME"] = destinationName;
            dr["LICENSE_PLATE_NUMBER"] = vehicleNumber;
            dr["CERTIFICATE_NUMBER"] = recordNumber;
            dr["VEHICLE_TYPE"] = vType;
            dr["BLOCK_TON"] = vSeats;
            dr["OPERATING_RANGE"] = businessScope;
            dr["ISSUING_DATA"] = issuingDate;
            dr["DOCUMENT_IMAGE"] = recordPicture;
            ds.Tables[0].TableName = this.TableName;
            this.Update(ds);

            return "OK";
        }

        /// <summary>
        /// 根据id获取 备案证信息
        /// </summary>
        /// <param name="recordid">备案证ID</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-14 9:45
        public DataSet GetRecordById(string recordid)
        {
            if (string.IsNullOrEmpty(recordid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("recordid", ParameterDirection.Input, EDbType.String, 36, recordid));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT [DJK_RECORD_CARD_ID],[OWNER_NAME],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[LICENSE_PLATE_NUMBER],[CERTIFICATE_NUMBER],[VEHICLE_TYPE],[BLOCK_TON],[OPERATING_RANGE],[ISSUING_DATA],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_RECORD_CARD](NOLOCK)  WHERE [DJK_RECORD_CARD_ID]=@recordid", parmCollection);
            ds.Tables[0].Columns.Add("VEHICLE_BRAND_NAME", typeof(string));

            for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(CommonEnum.VehicleBrandEnum),   Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_TYPE"]));
               
            }
            return ds;
        }

        /// <summary>
        /// 根据地接客id获取 备案证信息
        /// </summary>
        /// <param name="djackId">地接客ID</param>
        /// <returns></returns>
        /// Author:任延华
        /// Creation date:2015-9-24
        public DataSet GetRecordDjackId(string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT [DJK_RECORD_CARD_ID],[OWNER_NAME],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[LICENSE_PLATE_NUMBER],[CERTIFICATE_NUMBER],[VEHICLE_TYPE],[BLOCK_TON],[OPERATING_RANGE],[ISSUING_DATA],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_RECORD_CARD](NOLOCK)  WHERE [BELONGS_USERID]=@djackId AND [IS_DELETE]=0", parmCollection);
            ds.Tables[0].Columns.Add("VEHICLE_BRAND_NAME", typeof(string));

            for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(CommonEnum.VehicleBrandEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_TYPE"]));

            }
            return ds;
        }
    }
}
